
'use strict';

const Controller = require('egg').Controller;
const moment = require("moment");

class BaseController extends Controller {

    async userId() {
        const { ctx, app } = this;
        const token = ctx.request.header.token;
        const userId = await app.redis.get(token);
        this.vipExpire(userId);
        return userId;
        // return 1;
    }

    async vipExpire(userId) {
        const user = await this.ctx.model.User.findByPk(userId, {
            attributes: ['id', 'use_like', 'vip', 'open_time', 'expire_time']
        });
        const now = moment().format("YYYY-MM-DD HH:mm:ss")
        if (user && user.vip && moment(user.expire_time) <= moment(now)) {
            console.log('vip过期了');
            //vip过期
            user.vip = false;
            user.save();
        }
    }

}

module.exports = BaseController;